/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.unpack.file.utils;

import cn.ac.iie.mqSender.DistriThread;
import cn.ac.iie.unpack.file.global.GlobalParas;
import cn.ac.iie.unpack.file.main.UnpackFileMain;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import io.netty.util.concurrent.Future;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;

/**
 *
 * @author gjw
 */
public class FileUntarAndMoveThread implements Callable<Integer> {

    public static final Logger LOG = Logger.getLogger(FileUntarAndMoveThread.class);
    public volatile static boolean Stop;
    File outputDir;

    public FileUntarAndMoveThread(File outputDir) {
        this.outputDir = outputDir;
    }

    @Override
    public Integer call() {
        FileUntarAndMove fileUntar = new FileUntarAndMove();
        List<String> untaredFiles = null;
        File tarFile = null;
        while (!FileListGetThread.Stop||!GlobalParas.FILE_QUEUE.isEmpty()) {
            LOG.info("file queue num: "+GlobalParas.FILE_QUEUE.size());
            try {
                String fileName = GlobalParas.FILE_QUEUE.poll(3, TimeUnit.SECONDS);
                
                if(fileName == null){
                    Thread.sleep(5000);
                    continue;
                }
                LOG.info("read 1 file. name: " + fileName);
                tarFile = new File(fileName);
            } catch (InterruptedException ex) {
                LOG.error(ExceptionUtils.getFullStackTrace(ex));
            }
            //untar
            if (tarFile != null) {
                try {
                    LOG.info("start untar");
                    untaredFiles = fileUntar.unTar(tarFile, outputDir);
                } catch (ArchiveException ex) {
                    LOG.error(ExceptionUtils.getFullStackTrace(ex));
                    LOG.error(outputDir.getAbsolutePath());
                } catch (IOException ex) {
                    LOG.error(ExceptionUtils.getFullStackTrace(ex));
                    LOG.error(outputDir.getAbsolutePath());
                } catch (Exception ex) {
                    LOG.error(ExceptionUtils.getFullStackTrace(ex));
                    LOG.error(outputDir.getAbsolutePath());
                }
            }

            //copy and move
            if (untaredFiles.size() != 0) {
                try {
                    fileUntar.move(tarFile, GlobalParas.SUS_UNTAR_DIR);
                } catch (IOException ex) {
                    LOG.error(ExceptionUtils.getFullStackTrace(ex));
                }
                
                try {
                    fileUntar.delete(new File(tarFile.getAbsoluteFile() + GlobalParas.FILE_SUFFIX), GlobalParas.SUS_UNTAR_DIR);
                } catch (IOException ex) {
                    LOG.error(ExceptionUtils.getFullStackTrace(ex));
                }
                BufferedReader br = null ;
                String line=null;
                try {
                    br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(untaredFiles.get(0))),"utf-8"));
                    List list=new ArrayList();
                    while((line=br.readLine())!=null){
                        try{
                            JSONObject json=JSON.parseObject(line);
                            list.add(json);
                        }catch(JSONException ex){
                            LOG.error(tarFile.getAbsoluteFile());
                            LOG.error(untaredFiles);
                            LOG.error(line);
                        }catch(Exception ex){
                            LOG.error(tarFile.getAbsoluteFile());
                            LOG.error(untaredFiles);
                            LOG.error(line); 
                        }
                    }
                    DistriThread.msgQueue.put(list);
                }catch(Exception ex){
                    LOG.error(ExceptionUtils.getFullStackTrace(ex));
                }finally {
                    try {
                        br.close();
                    } catch (IOException ex) {
                        LOG.error(ExceptionUtils.getFullStackTrace(ex));
                    }
                }

            }
        }
        return 1;
    }
    public void endhandle(List<String> untaredFiles){
        FileUntarAndMove fileUntar = new FileUntarAndMove();
        try {
            for(String untaredFile:untaredFiles){
                fileUntar.copy(new File(untaredFile), GlobalParas.DISTRI_DESTINATION_DIR);
            }
        } catch (IOException ex) {
            LOG.error(ExceptionUtils.getFullStackTrace(ex));
        }
        try {
            for(String untaredFile:untaredFiles){
                fileUntar.createOkFile(untaredFile,GlobalParas.DISTRI_DESTINATION_DIR);
            }
        } catch (Exception ex) {
            LOG.error(ExceptionUtils.getFullStackTrace(ex));
        }
    }
    public static void main(String[] args) {
        Map map=new HashMap();
        map.put("a","");
//        JSONObject ss=new JSONObject();
//        String[]str={};
//        ss.put("s",str);
//        JSONObject tt=JSONObject.parseObject(ss.toString());
        System.out.println(map);
    }
}

